﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpeechLib;
namespace Gds.Utility.Helper
{
    public static class VoiceHelper
    {
        public static bool stopFlag = false;
        private static SpVoiceClass voice = null;

        public static void InitVoiceHelper()
        {
            BuildSpeach();
            SetChinaVoice();
        }

        private static void BuildSpeach()
        {
            if (voice == null)
                voice = new SpVoiceClass();

        }
        public static void ReBuildSpeach()
        {

            voice = new SpVoiceClass();

        }
        /// <summary>
        /// SRSEDone SRSEIsSpeaking
        /// </summary>
        /// <returns></returns>
        public static string GetVoiceStatue()
        {
            if (voice != null)
            {
                return voice.Status.RunningState.ToString();
            }
            else
            {
                return "1";
            }

        }
        private static void SetChinaVoice()
        {
            SetVoiceType("Language = 804");
        }
        private static void SetVoiceType(string languagetype)
        {
            ISpeechObjectTokens sps = voice.GetVoices(languagetype, string.Empty);
            //ISpeechObjectTokens sps1 = voice.GetVoices(string.Empty, string.Empty);
            //foreach (ISpeechObjectToken sp in sps1)
            //{
            //    if (sp.Id.IndexOf("MSMary", StringComparison.OrdinalIgnoreCase) >= 0)
            //    {
            //        voice.Voice = sp as SpObjectToken;
            //        return;
            //    }
            //}
            if (sps.Count > 0)
                voice.Voice = sps.Item(0);
        }
        /// <summary>
        /// 中文语音
        /// </summary>
        /// <param name="strSpeak"></param>
        public static void SpeakChina(string strSpeak)
        {
            //ViewModel.MainViewModel.Instance. AddLogModel("正在播报=>["+ strSpeak+"]" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));

            if (voice == null)
                voice = new SpVoiceClass();
            //voice.Resume();
            //SetChinaVoice();
            Speak(strSpeak);
        }
        public static void PauseVoice()
        {
            if (voice == null)
                voice = new SpVoiceClass();

            voice.Pause();
        }
        private static void Speak(string strSpeack)
        {
            //return;//TODO:暂停朗读
            //if (ViewModel.MainViewModel.Instance.IsVoiceOpen == false)
            //{
            //    voice.Pause();
            //    return;
            //}
            //else
            {
                voice.Resume();
            }
            if (voice == null) return;
            try
            {

                voice.Speak(strSpeack, SpeechVoiceSpeakFlags.SVSFlagsAsync);
                //int lastError = Marshal.GetLastWin32Error();


            }
            catch
            {
                //throw (new Exception("发生一个错误：" + err.Message));
            }
        }
        public static void StopSpeak()
        {
            try
            {
                if (voice != null)
                {
                    if (VoiceHelper.GetVoiceStatue() == "SRSEIsSpeaking" || VoiceHelper.GetVoiceStatue() == "0")
                    {
                        voice.Speak(string.Empty, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.Helper.PublicHelper.WriteErrorMsg(ex);
            }
            //voice = new SpVoiceClass();
            //SetChinaVoice();


        }
        public static void Speak(string text, int index, SpeechVoiceSpeakFlags flag = SpeechVoiceSpeakFlags.SVSFDefault)
        {
            try
            {
                SpVoice voice = new SpVoice();
                voice.Voice = voice.GetVoices(string.Empty, string.Empty).Item(index);
                voice.Speak(text, flag);
            }
            catch (Exception)
            {
            }
        }
    }
}
